import matplotlib.pyplot as plt


def drawTest():
    plt.rcParams['font.sans-serif'] = 'SimHei'  # 设置中文显示
    plt.figure(figsize=(6, 6))  # 将画布设定为正方形，则绘制的饼图是正圆
    label = ['第一', '第二', '第三']  # 定义饼图的标签，标签是列表
    explode = [0.01, 0.2, 0.01]  # 设定各项距离圆心n个半径
    # plt.pie(values[-1,3:6],explode=explode,labels=label,autopct='%1.1f%%')#绘制饼图
    values = [4, 7, 9]
    plt.pie(values, explode=explode, labels=label, autopct='%1.1f%%')  # 绘制饼图
    plt.title('2018年饼图')  # 绘制标题
    plt.savefig('./2018年饼图')  # 保存图片
    plt.show()


def calTimeSeq(t1: str, t2: str) -> int:
    """
    计算格式化时间(例如1632)的差值, 单位min
    :param t1: 起始时间
    :param t2: 终结时间
    :return: 差值
    """
    h1 = int(t1[:2])
    h2 = int(t2[:2])
    m1 = int(t1[2:])
    m2 = int(t2[2:])
    return (h2 - h1) * 60 + (m2 - m1)


def drawPie(date):
    with open(f"./data/{date}.txt", 'r') as f:
        lines = f.readlines()
        timeSeq = []
        attr = []
        for line in lines:
            sep = line.split(' ')
            timeSeq.append(calTimeSeq(*sep[0].split('~')))
            attr.append(sep[1])
    print(timeSeq)
    assert sum(timeSeq) == 1440
    print(attr)
    dic = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0}
    for i in range(len(timeSeq)):
        dic[attr[i][0]] += timeSeq[i]
    print(dic)

    plt.rcParams['font.sans-serif'] = 'SimHei'  # 设置中文显示
    plt.figure(date, figsize=(6, 6))  # 将画布设定为正方形，则绘制的饼图是正圆
    label = ['学习', '睡觉', '锻炼', '杂项', '娱乐']  # 定义饼图的标签，标签是列表
    explode = [0.01, 0.01, 0.01, 0.01, 0.01]  # 设定各项距离圆心n个半径

    # plt.pie(values[-1,3:6],explode=explode,labels=label,autopct='%1.1f%%')#绘制饼图
    def make_autopct(values):
        def my_autopct(pct):
            total = sum(values)
            val = int(round(pct * total / 100.0))
            # 同时显示数值和占比的饼图
            return '{p:.2f}%  ({h:d}:{m:d})'.format(p=pct, h=val // 60, m=val % 60)

        return my_autopct

    plt.pie(dic.values(), explode=explode, labels=label, autopct=make_autopct(dic.values()))  # 绘制饼图
    plt.title(f'陈柏居{date}时间使用情况')  # 绘制标题
    # plt.savefig('./2018年饼图')  # 保存图片


if __name__ == '__main__':
    data_in = eval(input("输入格式(%d, %d, %d...):"))
    if type(data_in) == int:
        data_in = (data_in,)
    for d in data_in:
        drawPie(str(d))
    plt.show()
